using System;
using System.Collections.Generic;
using System.Text;
using QLHXNK.DataAccess;
using System.Data;

namespace QLHXNK.BusinessLogic
{
    class ImportNote
    {
        private int importNoteID;
        private String importNoteCode;
        private DateTime importNoteDate;
        private double exchangeRate;
        private String remark;
        #region Properties
        public int ID
        {
            get { return importNoteID; }
        }
        public String Code
        {
            get { return importNoteCode; }
            set { importNoteCode = value; }
        }
        public DateTime Date
        {
            get { return importNoteDate; }
            set { importNoteDate = value; }
        }
        public double ExchangeRate
        {
            get { return exchangeRate; }
            set { exchangeRate = value; }
        }
        public String Remark
        {
            get { return remark; }
            set { remark = value; }
        }
        #endregion

        public ImportNote(int importNoteID, String importNoteCode, DateTime importNoteDate,double exchangeRate, String remark)
        {
            this.importNoteID = importNoteID;
            this.importNoteCode = importNoteCode;
            this.importNoteDate = importNoteDate;
            this.exchangeRate = exchangeRate;
            this.remark = remark;
        }

        #region ----------------------------------------Lấy thông tin hàng-----------------------------------------

        public static DataTable ListImportNote(int type)
        {
            try
            {
                return ImportNoteData.GetImportNote("",type);
            }
            catch
            {
                return null;
            }
        }

        public static DataTable GetImportNoteByCode(String importNoteCode)
        {
            try
            {
                return ImportNoteData.GetImportNote(importNoteCode,-1);
            }
            catch
            {
                return null;
            }
        }

        public static String GetImportNoteNo(DateTime importNoteDate)
        {
            try
            {
                DataTable dtImportNote = ImportNoteData.GetOrderOnPresentMonth(importNoteDate);
                if (dtImportNote != null)
                {
                    String strSTT = "";
                    if (dtImportNote.Rows.Count > 0)
                    {
                        strSTT = dtImportNote.Rows[dtImportNote.Rows.Count - 1]["import_note_code"].ToString();
                        strSTT = strSTT.Substring(strSTT.LastIndexOf("/") + 1);
                        int STT = Int32.Parse(strSTT) + 1;
                        strSTT = STT.ToString();
                    }
                    else
                        strSTT = "0001";
                    String month = importNoteDate.Month.ToString();
                    
                    if (month.Length == 1)
                        month = "0" + month;
                    if (strSTT.Length == 1)
                        strSTT = "000" + strSTT;
                    if (strSTT.Length == 2)
                        strSTT = "00" + strSTT;
                    if (strSTT.Length == 3)
                        strSTT = "0" + strSTT;
                    return "DH/" + month + importNoteDate.Year.ToString() + "/" + strSTT;
                }
                return "";
            }
            catch 
            {
                return "";
            }
        }

        #endregion

        #region ------------------------------------------Thêm, xoá, sửa-------------------------------------------

        public static ImportNote CreateImportNote(String importNoteCode, DateTime importNoteDate, double exchangeRate, String remark, int type)
        {
            try
            {
                int id = ImportNoteData.CreateImportNote(importNoteCode, importNoteDate, exchangeRate, remark, type);
                ImportNote importNote = null;
                if (id != 0)
                    importNote = new ImportNote(id, importNoteCode, importNoteDate, exchangeRate, remark);
                return importNote;
            }
            catch
            {
                return null;
            }
        }

        public int DeleteImportNote()
        {
            try
            {
                return ImportNoteData.DeleteImportNote(this.importNoteID);
            }
            catch
            {
                return 0;
            }
        }

        public int Save()
        {
            try
            {
                return ImportNoteData.UpdateImportNote(this.importNoteID,this.importNoteCode, this.importNoteDate, this.exchangeRate, this.remark);
            }
            catch
            {
                return 0;
            }
        }

        public int UpdateImportNote(String importNoteCode, DateTime importNoteDate, double exchangeRate, String remark)
        {
            try
            {
                this.importNoteCode = importNoteCode;
                this.importNoteDate = importNoteDate;
                this.exchangeRate = exchangeRate;
                this.remark = remark;
                return Save();
            }
            catch
            {
                return 0;
            }
        }

        #endregion
    }
}
